// pages/toagent/toagent.js
import { Agent } from '../agent/agent-model.js'
import { My } from '../my/my-model.js'
var agent = new Agent();
var my = new My();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		name: '',
		phone: '',
		idCard: '',
		loadingHidden: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var that = this;
		wx.login({
			success: function (res) {
				if (res.code) {
					//发起网络请求  
					console.log(res.code)
					that.setData({
						code: res.code
					});
				} else {
					console.log('获取用户登录态失败！' + res.errMsg)
				}
			}
		});
		my.getUser((data) => {
			var reGetPhone = false;
			if (data.phone.length == 11) {
				reGetPhone = true;
			}
			that.setData({
				loadingHidden: true,
				name: data.name,
				phone: data.phone,
				idCard: data.idcard,
				reGetPhone: reGetPhone,
			})
		})
	},

	getname: function (e) {
		this.setData({
			name: e.detail.value
		});
	},
	getPhone: function (e) {
		this.setData({
			phone: e.detail.value
		})
	},
	getIdCard: function (e) {
		this.setData({
			idCard: e.detail.value
		})
	},
	toAgent: function () {
		if (this.checkInput()) {
			agent.toAgent(this.data.name, this.data.idCard, (data) => {
				if (data.err_msg) {
					this.showModel(data.err_msg);
				}
				if (data.referral_code) {
					wx.showModal({
						title: '提示',
						content: '恭喜您成为我们的代理用户，您的专属推荐码为' + data.referral_code,
						success: function (res) {
							wx.setStorageSync('my_reload', true);
							wx.redirectTo({
								url: '../agent/agent',
							})
						}
					})
				}
			})
		}
	},
	getPhoneNumber: function (e) {
		var that = this;
		wx.login({
			success: function (res) {
				if (res.code) {
					that.setData({
						code: res.code
					});
				} else {
					console.log('获取用户登录态失败！' + res.errMsg)
				}
			}
		});
		if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
			wx.showModal({
				title: '提示',
				showCancel: false,
				content: '请授权使用您的手机号，以验证您的身份',
				success: function (res) { }
			})
		} else {
			agent.getUserPhone(that.data.code, e.detail.iv, e.detail.encryptedData, (data) => {
				console.log(data);
				if (data.err_msg) {
					that.showModel(data.err_msg);
					return;
				}
				that.setData({
					phone: data
				})
			});
		}
	},

	checkInput: function () {
		var name = this.data.name;
		var phone = this.data.phone;
		var idCard = this.data.idCard;
		var validateName = this.validateName(name);
		var validatephone = this.validatemobile(phone);
		var validateIdCard = this.validateIdCard(idCard);
		console.log(validateName)
		console.log(validatephone)
		console.log(validateIdCard)
		if (!validateName || !validatephone || !validateIdCard) {
			console.log("fail")
			return false;
		}
		return true;

	},

	showModel: function (msg) {
		wx.showModal({
			title: '提示',
			content: msg,
		})
	},

	validateName: function (name) {
		var result = {
			'pass': true,
			'msg': '验证成功'
		}
		// [\u4E00-\uFA29]|[\uE7C7-\uE7F3]汉字编码范围 
		var re1 = /^[\u4e00-\u9fa5]{2,4}$/;
		if (name == '') {
			result = {
				'pass': false,
				'msg': '请输入正确姓名'
			}
		}
		if (!re1.test(name)) {
			result = {
				'pass': false,
				'msg': '请输入正确姓名'
			}
		}
		console.log(result)
		if (!result.pass) {
			this.showModel(result.msg);
			return false;
		}
		return true;
	},

	validateIdCard: function (idCard) {
		var validate = agent.idCardValidate(idCard);
		console.log(validate)
		if (!validate.pass) {
			this.showModel(validate.msg);
			return false;
		} else {
			return true;
		}
	},

	validatemobile: function (mobile) {
		var result = {
			'pass': true,
			'msg': '验证成功'
		}
		if (mobile.length == 0 || mobile == null) {
			result = {
				'pass': false,
				'msg': '请输入手机号！'
			}
		}
		if (mobile.length != 11) {
			result = {
				'pass': false,
				'msg': '手机号长度有误！'
			}
		}
		var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
		if (!myreg.test(mobile)) {
			result = {
				'pass': false,
				'msg': '手机号格式校验有误！'
			}
		}
		console.log(result)
		if (!result.pass) {
			this.showModel(result.msg);
			return false;
		}
		return true;

	},
	//分享效果
	onShareAppMessage: function () {
		var referral_code = wx.getStorageSync('referral_code');
		return {
			title: '唐潮盐匠-自贡冷吃系列',
			path: 'pages/home/home?referral_code=' + referral_code
		}
	}

})